import {
  getFtmAirlineApi,
  getFtmAirlineMonitorInfoApi,
  getEventListAllApi,
  getFtmPlaneModelApi,
  getFtmAirplaneListApi,
  getAtaListListApi,
  getFtmAirportListApi,
  getFtmFleetApi,
  getFlightLegDetailApi,
  getRepReportTypeApi,
  getRepReportApi,
  getTemplateNameApi,
  getFlightLegHistoryListApi,
  getEventListByReportTypeLegIdApi,
  getEventDetailApi,
  getListSnapshotByParamApi,
  getListByModelApi,
  repUplinkDestinationApi,
  repUplinkRequestApi,
  addRepUplinkMessageApi,
  getRepUplinkMessageListApi,
  getEventItemProcessingApi,
  getCodeListApi,
  modifyAirportRemarkApi,
  getSummaryItemsApi,
  getRepUplinkDestinationApi,
  addRepUplinkDestinationApi,
  deleteBatchListApi,
  updateBatchListApi,
  repAnalysisJsonApi,
  getAirportDetailApi,
  getEventByLegIdApi,
  getFormattedMessageApi
} from '@/services/fraMonitoring.js';
import { delay } from '@/utils/util';
import { message } from 'ant-design-vue';
// import testData from './test.json';
export default {
  namespaced: true,
  state: {
    // 航空公司
    ftmAirlineList: [],
    // 汇总项目
    SummaryItemsList: [],
    // 事件
    eventList: [],
    eventListFlat: [],
    // 机型
    ftmPlaneModelList: [],
    // 机队
    ftmFleetList: [],
    // 起飞
    depList: [],
    // 到达
    arrList: [],
    modifyAirportRemarkLoading: false,
    // ata
    ataListList: [],
    // 机号
    ftmAirplaneList: [],
    // 航空监控
    ftmAirlineMonitorInfo: [],
    // 航空监控loading
    ftmAirlineMonitorInfoLoading: false,
    paginationOptions: {
      pageNum: 1,
      pageSize: 40,
      total: 0
    },
    // 当前航段详情
    flightLegDetailLoading: false,
    flightLegDetailData: [],
    flightLegDetailReportList: [],
    flightLegDetailReportTypes: [],
    flightLegDetailReportListNum: [],
    flightLegDetailReportListLoading: false,
    // 最近历史航段信息
    flightLegHistoryList: [],
    flightLegHistoryLoading: false,
    // 获取EF或Fault Summary报文列表
    eventListByReportTypeLegIdLoading: false,
    eventListByReportTypeLegIdData: [],
    // 获取航段事件详细信息
    getEventDetailLoading: false,
    getEventDetailData: [],
    // 快照
    repReportParamGroup: [],
    repReportType: [],
    snapshotLoading: false,
    snapshotList: [],
    // 上传报文
    repUplinkDestination: [],
    repUplinkRequest: [],
    addRepUplinkMessageLoading: false,
    repUplinkMessageListLoading: false,
    repUplinkMessageList: [],
    // 任务研究
    eventItemProcessingLoading: false,
    eventItemProcessingList: [],
    codeListList: [],
    codeListLoading: false,
    // 报文上传-管理供应商
    repUplinkDestinationLoading: false,
    repUplinkDestinationList: [],
    addRepUplinkDestinationLoading: false,

    templateNameList: [],
    clientWidth: 1366,
    repAnalysisJsonData: '',
    repAnalysisJsonTitle: {},
    repAnalysisJsonLoading: false,
    ftmAirplaneListFilter: [],
    ftmFleetListFilter: [],
    arrListFilter: [],
    depListFilter: [],

    // 勤务里的
    servftmPlaneModelList: [],
    servftmFleetListFilter: [],
    servftmAirplaneListFilter: [],

    efReportVisible: false,
    faultReportVisible: false,
    flightLegDetailsVisible: false,
    flightLegHistoryVisible: false,
    messageUploadVisible: false,
    snapshotVisible: false,
    eventDetailsVisible: false,
    drawerVisible: false,
    airportInfoVisible: false,
    legInfoVisible: false,
    activeIndex: [-1],
    faActiveIndex: [-1],
    modalZIndex: 2001,
    airportDetailFra: {},
    getAirportDetailLoading: false,
    mmsgCodeListList: [],
    fdeCodeListList: [],
    alertCodeListList: [],
    eventListByLegIdLoading: false,
    eventListByLegIdData: [],
    repAnalysisJsonEnding: '',
    cfdFormat: []
  },
  getters: {
    efReportVisibleF(state) {
      return state.efReportVisible;
    },

    faultReportVisibleF(state) {
      return state.faultReportVisible;
    },
    legInfoVisibleF(state) {
      return state.legInfoVisible;
    },
    flightLegDetailsVisibleF(state) {
      return state.flightLegDetailsVisible;
    },
    flightLegHistoryVisibleF(state) {
      return state.flightLegHistoryVisible;
    },
    messageUploadVisibleF(state) {
      return state.messageUploadVisible;
    },
    snapshotVisibleF(state) {
      return state.snapshotVisible;
    },
    eventDetailsVisibleF(state) {
      return state.eventDetailsVisible;
    },
    airportInfoVisibleF(state) {
      return state.airportInfoVisible;
    }

  },
  mutations: {
    setState(state, { key, payload }) {
      if ([
        'flightLegDetailsVisible',
        'efReportVisible',
        'faultReportVisible',
        'legInfoVisible',
        'flightLegHistoryVisible',
        'messageUploadVisible',
        'snapshotVisible',
        'eventDetailsVisible',
        'airportInfoVisible'].includes(key) && state[key] === true && payload === true) {
        state[key] = undefined;
        setTimeout(() => {
          state[key] = payload;
        }, 0);
      } else {
        state[key] = payload;
      }
    }
  },
  actions: {

    // 获取航空监控
    async getFtmAirlineMonitorInfo({ commit, state, dispatch }, params = {}) {
      if (!params.isAuto) {
        commit('setState', { key: 'ftmAirlineMonitorInfoLoading', payload: true });
      }

      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        isAuto: 0,
        ...params
      };
      const rep = await getFtmAirlineMonitorInfoApi(apiParams);
      // console.log(rep);
      // const rep2 = testData;
      dispatch('setting/setFraAcarsTime', {}, { root: true });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data?.total ?? 0 } });
      commit('setState', { key: 'ftmAirlineMonitorInfo', payload: rep.data?.list ?? [] });
      if (!params.isAuto) {
        commit('setState', { key: 'ftmAirlineMonitorInfoLoading', payload: false });
      }
    },
    // 获取汇总项
    async getSummaryItems({ commit }) {
      const rep = await getSummaryItemsApi();
      const newData = (rep.data ?? []).sort((a, b) => (a?.summary ?? 0).localeCompare((b?.summary ?? 0)));
      commit('setState', { key: 'SummaryItemsList', payload: newData });
    },
    // 获取航空公司列表
    async getFtmAirline({ commit, state }) {
      const rep = await getFtmAirlineApi();
      commit('setState', { key: 'ftmAirlineList', payload: rep.data });
    },
    // 获取事件类型Node列表
    async getEventList({ commit }) {
      const rep = await getEventListAllApi();
      /**
       *
       * @param {*} list 每层元数据
       * @param {*} listTmp 内存中每层的children
       * @param {*} eventType 最顶层eventType
       * @param {*} treeD 防止key重复
       * @param {*} listFlat 铺平的list，用来最后筛选顶层eventType
       * @returns
       */

      function eTypeFor(list, listTmp = [], eventType = '', treeD = 1, listFlat = []) {
        for (const iterator of list) {
          if (iterator.eventType) {
            eventType = iterator.eventType;
          }
          const nodeItem = {
            titleShow: iterator.eventType || iterator.name || '空值',
            value: `${iterator.eventType || iterator.name || '空值'}|${treeD}`,
            key: `${iterator.eventType || iterator.name || '空值'}|${treeD}`,
            subName: [],
            belong: eventType,
            scopedSlots: { title: 'title' }
          };

          if (iterator.childrenType?.length > 0) {
            nodeItem['children'] ? nodeItem['children'] : nodeItem['children'] = [];
            // eslint-disable-next-line no-debugger
            // debugger;
            if (iterator.childrenType[0]?.subName) {
              const listTmpI = listTmp.findIndex(item => item.titleShow === nodeItem.titleShow);
              if (listTmpI > -1) {
                listTmp[listTmpI]['subName']?.push(iterator.childrenType[0]?.subName);
              } else {
                nodeItem['subName'].push(iterator.childrenType[0]?.subName);
                listTmp.push(nodeItem);
                listFlat.push(nodeItem);
                treeD += 1;
              }
            } else {
              listTmp.push(nodeItem);
              listFlat.push(nodeItem);
              treeD += 1;
              eTypeFor(iterator.childrenType, nodeItem['children'], eventType, treeD, listFlat);
            }
          } else {
            listTmp.push(nodeItem);
            listFlat.push(nodeItem);
          }
        }
        return [listTmp, listFlat];
      }
      rep.data.forEach(element => {
        element.childrenType = (element.childrenType ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      });
      // const newData = (rep.data ?? []).sort((a, b) => (a?.eventType ?? 0).localeCompare((b?.eventType ?? 0)));
      const data = eTypeFor(rep.data);
      commit('setState', { key: 'eventList', payload: data[0] ?? [] });
      commit('setState', { key: 'eventListFlat', payload: data[1] ?? [] });
    },
    // 获取机型列表
    async getFtmPlaneModelList({ commit }) {
      const rep = await getFtmPlaneModelApi();
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'ftmPlaneModelList', payload: newData });
      commit('setState', { key: 'servftmPlaneModelList', payload: newData });
    },
    // 勤务信息里的
    async getFtmPlaneModelListFilterServ({ commit }, params) {
      const rep = await getFtmPlaneModelApi(params);
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'servftmPlaneModelList', payload: newData });
    },
    // 获取机队列表
    async getFtmFleet({ commit }) {
      const rep = await getFtmFleetApi();
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'ftmFleetList', payload: newData });
      commit('setState', { key: 'ftmFleetListFilter', payload: newData });
      commit('setState', { key: 'servftmFleetListFilter', payload: newData });
    },
    async getFtmFleetFilter({ commit }, params) {
      const rep = await getFtmFleetApi(params);
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'ftmFleetListFilter', payload: newData });
    },
    // 勤务信息里的
    async getFtmFleetFilterServ({ commit }, params) {
      const rep = await getFtmFleetApi(params);
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'servftmFleetListFilter', payload: newData });
    },
    // 获取尾号列表
    async getFtmAirplaneList({ commit }) {
      const rep = await getFtmAirplaneListApi();
      const newData = (rep.data ?? []).sort((a, b) => (a?.tailnumber ?? 0).localeCompare((b?.tailnumber ?? 0)));
      commit('setState', { key: 'ftmAirplaneList', payload: newData });
      commit('setState', { key: 'ftmAirplaneListFilter', payload: newData });
      commit('setState', { key: 'servftmAirplaneList', payload: newData });
    },
    async getFtmAirplaneListFilter({ commit }, params) {
      const rep = await getFtmAirplaneListApi(params);
      const newData = (rep.data ?? []).sort((a, b) => (a?.tailnumber ?? 0).localeCompare((b?.tailnumber ?? 0)));
      commit('setState', { key: 'ftmAirplaneListFilter', payload: newData });
    },
    // 勤务里的
    async getFtmAirplaneListFilterserv({ commit }, params) {
      const rep = await getFtmAirplaneListApi(params);
      const newData = (rep.data ?? []).sort((a, b) => (a?.tailnumber ?? 0).localeCompare((b?.tailnumber ?? 0)));
      commit('setState', { key: 'servftmAirplaneListFilter', payload: newData });
    },

    // 获取ATA列表
    async getAtaListList({ commit }) {
      const rep = await getAtaListListApi();
      const newData = (rep.data ?? []).sort((a, b) => +a.ataId - +b.ataId);
      newData.forEach(item => {
        item.nameF = item.name;
        if (item.name.split('-')[0] !== item.ataId) {
          item.nameF = item.ataId + '-' + item.name;
        }
      });

      commit('setState', { key: 'ataListList', payload: newData });
    },
    // 获取机场列表
    async getFtmAirportList({ commit }) {
      const a1 = getFtmAirportListApi({ type: 'dep' });
      const a2 = getFtmAirportListApi({ type: 'arr' });
      const rep = await Promise.all([a1, a2]);
      const newData1 = (rep[0].data ?? []).sort((a, b) => (a?.threeCode ?? 0).localeCompare((b?.threeCode ?? 0)));
      const newData2 = (rep[1].data ?? []).sort((a, b) => (a?.threeCode ?? 0).localeCompare((b?.threeCode ?? 0)));
      commit('setState', { key: 'depList', payload: newData1 });
      commit('setState', { key: 'arrList', payload: newData2 });
      commit('setState', { key: 'depListFilter', payload: newData1 });
      commit('setState', { key: 'arrListFilter', payload: newData2 });
    },
    async getFtmAirportListFilter({ commit }) {
      const a1 = getFtmAirportListApi({ type: 'dep' });
      const a2 = getFtmAirportListApi({ type: 'arr' });
      const rep = await Promise.all([a1, a2]);
      const newData1 = (rep[0].data ?? []).sort((a, b) => (a?.threeCode ?? 0).localeCompare((b?.threeCode ?? 0)));
      const newData2 = (rep[1].data ?? []).sort((a, b) => (a?.threeCode ?? 0).localeCompare((b?.threeCode ?? 0)));
      commit('setState', { key: 'depListFilter', payload: newData1 });
      commit('setState', { key: 'arrListFilter', payload: newData2 });
    },
    // 当前航段详情
    async getFlightLegDetail({ commit, dispatch, state }, payload) {
      commit('setState', { key: 'flightLegDetailLoading', payload: true });
      // 当前航段详情中报告
      const { tailNumber, legId, leg } = payload;
      const a1 = dispatch('getRepReport', { tailNumber, legId });
      const a2 = getFlightLegDetailApi({ tailNumber, leg });
      const repAll = await Promise.all([a1, a2]);
      commit('setState', { key: 'flightLegDetailReportTypes', payload: repAll[0]?.[0]?.data ?? [] });
      commit('setState', { key: 'flightLegDetailReportList', payload: repAll[0]?.[1]?.data.repReports ?? [] });
      commit('setState', { key: 'flightLegDetailReportListNum', payload: [state.flightLegDetailReportList.length, state.flightLegDetailReportList.length] });
      commit('setState', { key: 'flightLegDetailData', payload: repAll[1]?.data ?? [] });
      commit('setState', { key: 'flightLegDetailLoading', payload: false });
    },
    // 初始时获取当前航段详情中报告
    async getRepReport({ commit, state }, { tailNumber, legId }) {
      const rep1 = await getRepReportTypeApi();
      const apiParams = {
        category: rep1.data.flatMap(item => item.name),
        tailNumber,
        legId
      };
      const rep2 = await getRepReportApi(apiParams);
      return [rep1, rep2];
    },
    // 获取当前航段详情中报告
    async getRepReportS({ commit, state }, payload) {
      if (payload.category.length === 0) {
        commit('setState', { key: 'flightLegDetailReportList', payload: [] });
      } else {
        commit('setState', { key: 'flightLegDetailReportListLoading', payload: true });
        const rep = await getRepReportApi(payload);
        var dataList = rep.data.repReports
        console.log('dataList');
        var reportType11 = ''
        for (let i = 0; i < dataList.length; i++) {
          var reportType = dataList[i].reportType;
          if (reportType) {
            if (reportType === 'Flight Phase Change') {
              var content = dataList[i].content
              const result = (content.substr(content.indexOf('-  ') + 1, 10)).trim();
              const mid = result.substr(result.indexOf('1') + 1, 2)
              const lastS = result.substr(result.length - 2, result.length)
              let aa = [
                'Power On (PO)',
                'Engine Start (ES)',
                'Taxi Out (TA)',
                'Takeoff Roll (TO)',
                'lnitial Climb (IC)',
                'Climb (CL)',
                'Cruise (ER)',
                'Descent (DC)',
                'Approach (AP)',
                'Go Around (GA)',
                'Flare (FL)',
                'Rollout (RO)',
                'Taxi In (TI)',
                'Engine Shutdown (SD)',
                'Maintenance (MT)'
              ];

              let mid1 = aa.find(v => v.slice(-4).includes(mid))
              let lastS1 = aa.find(v => v.slice(-4).includes(lastS))
              reportType11 = reportType + ' - ' + mid1 + ' TO ' + lastS1
              Object.assign(dataList[i], {
                reportType: reportType11
              })
            }
          }
          console.log(dataList[i]);
        }
        commit('setState', { key: 'flightLegDetailReportList', payload: dataList ?? [] });
        commit('setState', { key: 'flightLegDetailReportListNum', payload: [state.flightLegDetailReportListNum[0], rep.data.length] });
        commit('setState', { key: 'flightLegDetailReportListLoading', payload: false });
      }
    },
    // 获取当前航段详情中报告
    async getFlightLegHistoryList({ commit }, payload) {
      commit('setState', { key: 'flightLegHistoryLoading', payload: true });
      const rep = await getFlightLegHistoryListApi(payload);
      commit('setState', { key: 'flightLegHistoryList', payload: rep.data });
      commit('setState', { key: 'flightLegHistoryLoading', payload: false });
    },
    // 获取EF或Fault Summary报文列表
    async getEventListByReportTypeLegId({ commit }, payload) {
      commit('setState', { key: 'eventListByReportTypeLegIdLoading', payload: true });
      const rep = await getEventListByReportTypeLegIdApi(payload);
      commit('setState', { key: 'eventListByReportTypeLegIdData', payload: rep.data });
      commit('setState', { key: 'eventListByReportTypeLegIdLoading', payload: false });
    },
    // 获取航段信息
    async getEventByLegId({ commit }, payload) {
      commit('setState', { key: 'eventListByLegIdLoading', payload: true });
      const rep = await getEventByLegIdApi(payload);
      commit('setState', { key: 'eventListByLegIdData', payload: rep.data });
      commit('setState', { key: 'eventListByLegIdLoading', payload: false });
    },
    // 获取航段事件详细信息
    async getEventDetail({ commit }, payload) {
      commit('setState', { key: 'getEventDetailLoading', payload: true });
      try {
        const rep = await getEventDetailApi(payload);

        commit('setState', { key: 'getEventDetailData', payload: rep.data });
      } finally {
        commit('setState', { key: 'getEventDetailLoading', payload: false });
      }
    },
    // 获取标签分组列表 下拉框
    async getRepReportParamGroup({ commit }, payload) {
      const rep = await getListByModelApi(payload);

      commit('setState', { key: 'repReportParamGroup', payload: rep?.data ?? [] });
    },
    // 获取报文类型列表
    async getRepType({ commit }) {
      const rep = await getRepReportTypeApi();
      commit('setState', { key: 'repReportType', payload: rep?.data ?? [] });
    },

    // 获取报告类型 下拉列表
    async getTemplateName({ commit }) {
      const rep = await getTemplateNameApi();
      const newData = (rep?.data ?? []).sort((a, b) => (a?.templateName ?? 0).localeCompare((b?.templateName ?? 0)));
      commit('setState', { key: 'templateNameList', payload: newData });
    },
    // 获取标签分组列表 下拉框/获取报文类型列表
    async getListSnapshotByParam({ commit }, payload) {
      commit('setState', { key: 'snapshotLoading', payload: true });
      const rep = await getListSnapshotByParamApi(payload);
      commit('setState', { key: 'snapshotList', payload: rep.data });
      commit('setState', { key: 'snapshotLoading', payload: false });
    },
    // 上传报文两个下拉
    async getUploadMessageConditions({ commit }, payload) {
      const a1 = repUplinkDestinationApi(payload);
      const a2 = repUplinkRequestApi();
      const repAll = await Promise.all([a1, a2]);
      const newData0 = (repAll[0]?.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      const newData = (repAll[1]?.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));

      commit('setState', { key: 'repUplinkDestination', payload: newData0 });
      commit('setState', { key: 'repUplinkRequest', payload: newData });
    },
    async getUploadMessageConditionsPri({ commit }, payload) {
      const rep = await repUplinkDestinationApi(payload);
      const newData0 = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'repUplinkDestination', payload: newData0 });
    },
    // 上传报文
    async addRepUplinkMessage({ commit }, payload) {
      commit('setState', { key: 'addRepUplinkMessageLoading', payload: true });
      try {
        const rep = await addRepUplinkMessageApi(payload);

        return Promise.resolve(rep);
      } finally {
        await delay(1000);
        commit('setState', { key: 'addRepUplinkMessageLoading', payload: false });
      }
    },
    // 获取报文列表
    async getRepUplinkMessageList({ commit }, payload) {
      commit('setState', { key: 'repUplinkMessageListLoading', payload: true });
      const rep = await getRepUplinkMessageListApi(payload);
      commit('setState', { key: 'repUplinkMessageList', payload: rep.data });
      commit('setState', { key: 'repUplinkMessageListLoading', payload: false });
    },
    // 任务研究
    async getEventItemProcessing({ commit, rootState }, payload) {
      commit('setState', { key: 'eventItemProcessingLoading', payload: true });
      const apiParams = {
        ...payload
      };

      payload.code && payload.code[0] === 'All' ? apiParams.code = rootState.fraMonitoring.fdeCodeListList.flatMap(item => item.code) : '';
      const rep = await getEventItemProcessingApi(apiParams);
      commit('setState', { key: 'eventItemProcessingList', payload: rep.data });
      commit('setState', { key: 'eventItemProcessingLoading', payload: false });
    },
    // 获取警告代码列表
    async getAlertCodeList({ commit }, payload) {
      commit('setState', { key: 'codeListLoading', payload: true });
      const rep = await getCodeListApi({ faultType: 'Alert' });
      commit('setState', { key: 'alertCodeListList', payload: rep.data });
      commit('setState', { key: 'codeListLoading', payload: false });
    },
    // 获取故障代码列表
    async getMMSGCodeList({ commit }, payload) {
      commit('setState', { key: 'codeListLoading', payload: true });
      const rep = await getCodeListApi({ faultType: 'MMSG' });
      commit('setState', { key: 'mmsgCodeListList', payload: rep.data });
      commit('setState', { key: 'codeListLoading', payload: false });
    },
    async getFDECodeList({ commit }, payload) {
      commit('setState', { key: 'codeListLoading', payload: true });
      const rep = await getCodeListApi({ faultType: 'FDE' });
      commit('setState', { key: 'fdeCodeListList', payload: rep.data });
      commit('setState', { key: 'codeListLoading', payload: false });
    },
    // 获取格式化报文
    async repAnalysisJson({ commit }, payload) {
      commit('setState', { key: 'repAnalysisJsonData', payload: '' });
      commit('setState', { key: 'repAnalysisJsonLoading', payload: true });
      try {
        const rep = await repAnalysisJsonApi(payload);
        let body = rep.body ?? '';
        const title = rep.title ?? '';
        let ending = rep.ending ?? '';
        if (!/<style type=/.test(body)) {
          body = `<pre>${body}</pre>`;
        }
        ending ? ending = `<pre>${ending}</pre>` : '';
        commit('setState', { key: 'repAnalysisJsonData', payload: body });
        commit('setState', { key: 'repAnalysisJsonEnding', payload: ending });
        commit('setState', { key: 'repAnalysisJsonTitle', payload: title });
      } finally {
        commit('setState', { key: 'repAnalysisJsonLoading', payload: false });
      }
    },

    async getFormattedMessage({ commit }, payload) {
      commit('setState', { key: 'repAnalysisJsonLoading', payload: true });
      const { reportId, imi = '' } = payload;
      const rep = await getFormattedMessageApi({ reportId });
      let newD = [...rep.data];
      if (imi) {
        newD = rep.data.filter(item => item.msgType === imi);
      }

      commit('setState', { key: 'cfdFormat', payload: newD });
      commit('setState', { key: 'repAnalysisJsonLoading', payload: false });
    },
    // 修改机场备注
    async modifyAirportRemark({ commit }, payload) {
      commit('setState', { key: 'modifyAirportRemarkLoading', payload: true });
      try {
        const handle = payload.map(item => modifyAirportRemarkApi(item));
        const repAll = await Promise.all(handle);

        const errItemIndex = repAll.findIndex(item => item.code !== 200);
        if (errItemIndex === -1) {
          message.success(repAll[0].message);
          return Promise.resolve(true);
        } else {
          message.success(repAll[errItemIndex].message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'modifyAirportRemarkLoading', payload: false });
      }
    },
    // 报文上传-管理供应商
    async getRepUplinkDestination({ commit }, payload) {
      commit('setState', { key: 'repUplinkDestinationLoading', payload: true });
      const rep = await getRepUplinkDestinationApi(payload);
      const newData = (rep?.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'repUplinkDestinationList', payload: newData });
      commit('setState', { key: 'repUplinkDestinationLoading', payload: false });
    },

    // 新增
    async addRepUplinkDestination({ commit }, params) {
      commit('setState', { key: 'addRepUplinkDestinationLoading', payload: true });
      try {
        const rep = await addRepUplinkDestinationApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepUplinkDestinationLoading', payload: false });
      }
    },
    async getAirportDetailFra({ commit, state }, params) {
      commit('setState', { key: 'getAirportDetailLoading', payload: true });
      const rep = await getAirportDetailApi(params);
      commit('setState', { key: 'airportDetailFra', payload: rep.data });
      commit('setState', { key: 'getAirportDetailLoading', payload: false });
    },

    // 删除
    async deleteRepUplinkDestination({ commit }, params) {
      try {
        await deleteBatchListApi({ ids: params });
        // if (rep.data === '成功') {
        message.success('操作成功');
        return Promise.resolve(true);
        // } else {
        //   message.error(rep.message);
        // }
      } catch (error) {
        message.error('操作失败');
      }
    },
    // 修改
    async updateRepUplinkDestination({ commit }, params) {
      commit('setState', { key: 'addRepUplinkDestinationLoading', payload: true });
      try {
        const rep = await updateBatchListApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepUplinkDestinationLoading', payload: false });
      }
    }

  }
};
